<template>
  <div>
    <!-- 搜索表单 -->
    <el-form :inline="true" @submit.native.prevent="fetchData" size="small">
      <el-form-item label="用户名">
        <el-input v-model="searchQuery.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 用户列表 -->
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="name" label="用户名"></el-table-column>
      <el-table-column prop="sex" label="性别">
        <template #default="scope">
          {{ scope.row.sex === '1' ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="birthday" label="出生日期"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="banned" label="账号状态">
        <template #default="scope">
          {{ scope.row.banned === '1' ? '封禁' : '正常' }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogVisible">
      <el-form :model="editForm">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="editForm.sex">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" :label-width="formLabelWidth">
          <el-date-picker
              v-model="editForm.birthday"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="用户状态" :label-width="formLabelWidth">
          <el-switch
              v-model="editForm.banned"
              active-value="0"
              inactive-value="1">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUser(editForm)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {selectUser} from "@/http/home/user/get";
import {updateUser} from "@/http/home/user/post";

export default {
  data() {
    return {
      searchQuery: {
        username: ''
      },
      userList: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      editDialogVisible: false,
      editForm: {
        id: '',
        name: '',
        sex: '',
        birthday: '',
        password: '',
        phone: '',
        email: '',
        roleLevel: '',
        createTime: '',
        banned: '',
      },
      formLabelWidth: '120px'
    };
  },
  methods: {
    fetchData() {
      // 更新 userList, total 等数据
      selectUser(this.searchQuery.username || 'null', this.currentPage, this.pageSize).then(res => {
        if (res.data.code == 200) {
          console.log(res.data.data);
          this.userList = res.data.data.records
          this.total = res.data.data.totalRow
        } else {
          this.$message.error(response.data.msg);
        }
      });
    },
    handleEdit(user) {
      console.log(user);
      this.editForm = { ...user };
      this.editDialogVisible = true;
    },
    updateUser(form) {
      // 这里调用API更新用户信息
      this.editDialogVisible = false;
      updateUser(form).then(res => {
        console.log(res);
        if (res.data.code = 200) {
          this.$message.success(res.data.data)
          this.fetchData('null', 1, this.pageSize);
          return;
        }
        this.$message.error(res.data.data)
      })
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.fetchData();
    },
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.fetchData();
    }
  },
  mounted() {
    this.fetchData();
  }
};
</script>
